package com.plsjavaweb.ui.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.plsjavaweb.core.AccountCore;
import com.plsjavaweb.pojo.AccountPOJO;
import com.plsjavaweb.logging.LoggingConstants;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	static Logger log = Logger.getLogger(Login.class);
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getSession().invalidate();
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.info(LoggingConstants.entryMessage);

		String inputUsername = request.getParameter("username");
		String inputPassword = request.getParameter("password");

		AccountPOJO account = new AccountPOJO();
		account.setPassword(inputPassword);
		account.setUsername(inputUsername);

		AccountCore accountCore = new AccountCore();
		accountCore.validateAccount(account);

		//Redirects to Dashboard Page and Initialize Session
		if (account.isValid()) {
			HttpSession session = request.getSession(true);
			session.setAttribute("user", account.getUsername());
			session.setAttribute("role", account.getRole());
			session.setAttribute("employeeId", account.getEmployeeId());
			//session.setAttribute("employeeId", new Integer(account.getEmployeeId()).toString());
			response.sendRedirect("Home");
		}
		
		//Set Validation Message in Request and Forward to Login Page
		else if (!account.isValid() && account.getValidationMessage().length() > 0) {
			request.setAttribute("inputUsername", inputUsername);
			request.setAttribute("inputPassword", inputPassword);
			request.setAttribute("validationMessage",
					account.getValidationMessage());
			request.getRequestDispatcher("index.jsp")
					.forward(request, response);
		}
		
		//Forward to Login Page
		else
		{
			request.getRequestDispatcher("index.jsp")
			.forward(request, response);
		}
		// String errorMessage = null;
		// String usernamePattern =
		// "[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
		//
		// if (inputUsername != null && inputUsername != "") {
		// if (inputUsername.matches(usernamePattern)) {
		// String correctPassword = account.getUserAccounts().get(
		// inputUsername);
		// if (correctPassword != null) {
		// boolean isPasswordCorrect = inputPassword
		// .equals(correctPassword);
		//
		// if (isPasswordCorrect) {
		// System.out.println("correct password");
		// response.sendRedirect("Home");
		// } else {
		// errorMessage = "password is not correct";
		// }
		// } else {
		// errorMessage = "Username does not exist";
		// }
		//
		// } else {
		// errorMessage = "input username is not in correct format";
		// }
		//
		// } else {
		// errorMessage = "Username cannot be null or empty";
		// }
	}

}
